
<?
include_once("../../database.php");
include_once("../../backend.php");
include_once("../../login_management.php");
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/scoutingevent.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<link rel="stylesheet" type="text/css" href="../../scouting.css"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Completed | Team Spyder Scouting</title>
<!-- InstanceEndEditable -->
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
</head>

<body>
<div class="main">
<div class="top"><l>
<a class="navigation" href="./index.php">Home</a> | 
<a class="navigation" href="./event.php">Event Management</a> | 
<a class="navigation" href="../index.php"> Admin </a> | 
<script type="text/javascript">
function LoadLogin()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("content").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","login.php",true);
xmlhttp.send();
}
</script>

<?
if($logged_in)
{
	echo "<l> Welcome, " . $_SESSION['username'] . " </l>";
	echo "<a class=\"navigation\" href=\"./logout.php\">Log out</a>\n";
}
else
{
	echo "<l> You are not logged in. </l>";
	echo "<a class=\"navigation\" href=\"#\" onclick=\"LoadLogin()\">Log in</a>\n";
}

?>
</l>
</div>
<div class="content"><!-- InstanceBeginEditable name="Content" -->
<?
#if($logged_in)
#{
if(isset($_POST['createseason'])) // specifies that the createevent.php form refered the user to this page.  run the script to create a new competition
{//init the table with a statement that looks like this: CREATE TABLE testcompetition (
	echo "<h3>Creating Year</h1>";
	echo "Creating Tables.........\n ";
	$numevents = $_POST['numevents'];
	$sql = "CREATE TABLE `" . $_POST['compname'] . "`  
	( Regional Varchar(500) NOT NULL,
	`Match` Int(200) NOT NULL,
	Team Int(200) NOT NULL,
	";
	//echo $sql . "-------\n";
	$numcols=$_POST['numcols']; //gets the number of collumns that are specified so that we dont forget any...
	for($i=1;$i<=$numcols;$i++) //for loop to assemble the query that assembles the table.  also sends some other queries into the tableattribs table
	{
		$q2 = "SELECT eventid FROM eventlist";
		$result = mysql_query($q2) or die(mysql_error());
		$eventid=1;
		while($row=mysql_fetch_array($result))
		{
			if($row['eventid']>=$eventid)
			{
				$eventid=$row['eventid']+1;
			}
		}
		for($j=1;$j<=$numevents;$j++)
		{
			$display=(@$_POST['field_'.$i.'_display']=="1")? $_POST['field_'.$i.'_display']:0;
			$predict=(@$_POST['field_'.$i.'_predict']=="1")? $_POST['field_'.$i.'_predict']:0;
			$chart=(@$_POST['field_'.$i.'_charts']=="1")? $_POST['field_'.$i.'_charts']:0;
			$team=(@$_POST['field_'.$i.'_team']=="1")? $_POST['field_'.$i.'_team']:0;
			$chart=(@$_POST['field_'.$i.'_match']=="1")? $_POST['field_'.$i.'_match']:0;
		$q="INSERT INTO eventvars values('" . $eventid . "', '" . $_POST['comptype'] . "', '" . $_POST['field_' . $i . '_width'] . "', '" . $_POST['field_' . $i . '_maxval'] . "', '" . $_POST['field_' . $i . '_name'] . "','" . $chart . "','" . $match . "','" . $team . "','" . $predict . "','" . $display . "')";
		mysql_query($q) or die(mysql_error());
		echo $q;
		#$q="INSERT INTO eventvars values('" . $eventid . "', '" . $_POST['comptype'] . "', 'maxval', '" . $_POST['field_' . $i . '_maxval'] . "', '" . $_POST['field_' . $i . '_name'] . "')";
		#mysql_query($q) or die(mysql_error());
		#echo $q;
		$eventid++;
		}
		//sleep(100);
		$sql=($sql . "`" . $_POST['field_' . $i . '_name'] . "` " . $_POST['field_' . $i . '_datatype'] . "(" . $_POST['field_' . $i . '_maxval'] . ") NOT NULL");  //assemble each field.  looking like this: matchnum varchar(500) NOT NULL
				//echo $sql . "-------\n\n\n";
		if($i!=$numcols)
		{
			$sql=$sql . ",\n"; //add a comma if its not the last one... keep good syntax
		}
		else
		{
			$sql=$sql . "\n";  //dont add a comma if its the last one...
		}
		//echo $sql . "-------\n\n\n";
	}
	$sql=$sql . ")";  //close the statement by using a )
	echo $sql;  
	$result=mysql_query($sql) or die(mysql_error());
	echo "Table Creation successful. ";
	
	echo "<h1>Creating Events</h1>";  //just tell the people whats going on...
	#$q2 = "SELECT eventid FROM eventlist";
	#$result = mysql_query($q2) or die(mysql_error());
	#$nr=mysql_num_rows($result);  //get the current number of events
	#$eventid = $nr+1;
	$q2 = "SELECT eventid FROM eventlist";
		$result = mysql_query($q2) or die(mysql_error());
		$eventid=1;
		while($row=mysql_fetch_array($result))
		{
			if($row['eventid']>=$eventid)
			{
				$eventid=$row['eventid']+1;
			}
		}
	for($i=1;$i<=$numevents;$i++) //enumerate events
	{
		$q="INSERT INTO eventvars values('" . $eventid . "', '" . $_POST['comptype'] . "', '40', '500', 'Team','0','0','0','0')";
		mysql_query($q) or die(mysql_error());
		echo $q."<br/>";
		#$q="INSERT INTO eventvars values('" . $eventid . "', '" . $_POST['comptype'] . "', 'maxval', '500', 'Team')";
		#mysql_query($q) or die(mysql_error());
		#echo $q."<br/>";
		$q="INSERT INTO eventvars values('" . $eventid . "', '" . $_POST['comptype'] . "', '40', '500', 'Match','0','0','0','0')";
		mysql_query($q) or die(mysql_error());
		echo $q."<br/>";
		#$q="INSERT INTO eventvars values('" . $eventid . "', '" . $_POST['comptype'] . "', 'maxval', '500', 'Match')";
		#mysql_query($q) or die(mysql_error());
		#echo $q."<br/>";
		$temp=$_POST['event_' . $i . '_name']; //get the temp value of the competition name
		$q="INSERT INTO eventlist VALUES ('" . $eventid . "','" . $_POST['compyear'] . "','" . $_POST['event_' . $i . '_name'] . "','" . $_POST['comptype'] . "', '" . $_POST['compname'] . "')"; //assemble the query
		mysql_query($q) or die(mysql_error()); //execute the query
		echo $q;
		$eventid++;  //increment eventid so that you dont have duplicate eventid's in the database.
		echo "Event Created <br />\n";
	}
	echo "Tasks completed.";
	
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(isset($_POST['createevent'])) // specifies that the createevent.php form refered the user to this page.  run the script to create a new competition
{//init the table with a statement that looks like this: CREATE TABLE testcompetition (
	$numevents=$_POST['numevents'];
	$q2 = "SELECT eventid FROM eventlist";
	$result = mysql_query($q2) or die(mysql_error());
	$eventid=1;
	while($row=mysql_fetch_array($result))
	{
		if($row['eventid']>=$eventid)
		{
			$eventid=$row['eventid']+1;
		}
	}
	$eventidorig=$eventid;
	for($i=1;$i<=$numevents;$i++)
	{
		$d1q="SELECT * FROM eventlist WHERE datatable='".$_POST['season']."' LIMIT 1";
		$d1r=mysql_query($d1q)or die(mysql_error());
		$d1a=mysql_fetch_array($d1r);
		$d2q="SELECT * FROM eventvars WHERE eventid='".$d1a['eventid']."'";
		$d2r=mysql_query($d2q) or die(mysql_error());
		while($d2a=mysql_fetch_array($d2r))
		{
			$d3q="INSERT INTO eventvars VALUES('".$eventid."','".$d2a['competition']."','".$d2a['width']."','".$d2a['maxval']."','".$d2a['fieldname']."','".$d2a['chart_chart']."','".$d2a['chart_team']."','".$d2a['chart_match']."','".$d2a['rank_predict']."','".$d2a['rank_dispaly']."')";
			$d3r=mysql_query($d3q)or die(mysql_error());
		}
	$eventid++;
	}
	$eventid=$eventidorig;
	echo "<h1>Creating Events</h1>";  //just tell the people whats going on...
	$q2 = "SELECT eventid FROM eventlist";
	$result = mysql_query($q2) or die(mysql_error());
	for($i=1;$i<=$numevents;$i++) //enumerate events
	{
		$temp=$_POST['event_' . $i . '_name']; //get the temp value of the competition name
		$q="INSERT INTO eventlist VALUES ('" . $eventid . "','" . $d1a['year'] . "','" . $_POST['event_'.$i.'_name'] . "','" . $d1a['competition'] . "', '" . $d1a['datatable'] . "')"; //assemble the query
		mysql_query($q) or die(mysql_error()); //execute the query
		echo $q;
		$eventid++;  //increment eventid so that you dont have duplicate eventid's in the database.
		echo "Event Created <br />\n";
	}
	echo "Tasks completed.";
	
}
#}
#else
#{
#	displayLogin();
#}
?>
<!-- InstanceEndEditable -->
</div>
</div>

</body>
<!-- InstanceEnd --></html>
